using System;
using System.Collections.Generic;
using System.IO;
using System.Media;
using System.Windows.Forms;

namespace HuaRongDaoGame.Services
{
    /// <summary>
    /// 音效服务
    /// </summary>
    public class SoundService
    {
        private Dictionary<string, SoundPlayer> soundPlayers;
        private bool isEnabled;
        private float volume;

        /// <summary>
        /// 音效启用状态改变事件
        /// </summary>
        public event EventHandler<bool> SoundEnabledChanged;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SoundService()
        {
            soundPlayers = new Dictionary<string, SoundPlayer>();
            isEnabled = true;
            volume = 1.0f;
            InitializeSounds();
        }

        /// <summary>
        /// 初始化音效
        /// </summary>
        private void InitializeSounds()
        {
            try
            {
                // 尝试加载音效文件，如果文件不存在则使用系统音效
                LoadSound("move", "sounds/move.wav");
                LoadSound("select", "sounds/select.wav");
                LoadSound("victory", "sounds/victory.wav");
                LoadSound("button", "sounds/button.wav");
                LoadSound("hint", "sounds/hint.wav");
            }
            catch (Exception)
            {
                // 如果加载失败，使用默认系统音效
                UseSystemSounds();
            }
        }

        /// <summary>
        /// 加载音效文件
        /// </summary>
        private void LoadSound(string name, string filePath)
        {
            if (File.Exists(filePath))
            {
                try
                {
                    var player = new SoundPlayer(filePath);
                    player.LoadAsync();
                    soundPlayers[name] = player;
                }
                catch (Exception)
                {
                    // 如果加载失败，忽略此音效
                }
            }
        }

        /// <summary>
        /// 使用系统音效
        /// </summary>
        private void UseSystemSounds()
        {
            // 使用系统音效作为备选
            try
            {
                SystemSounds.Asterisk.Play();
            }
            catch (Exception)
            {
                // 如果系统音效也失败，则静音
            }
        }

        /// <summary>
        /// 播放移动音效
        /// </summary>
        public void PlayMoveSound()
        {
            PlaySound("move");
        }

        /// <summary>
        /// 播放选择音效
        /// </summary>
        public void PlaySelectSound()
        {
            PlaySound("select");
        }

        /// <summary>
        /// 播放胜利音效
        /// </summary>
        public void PlayVictorySound()
        {
            PlaySound("victory");
        }

        /// <summary>
        /// 播放按钮音效
        /// </summary>
        public void PlayButtonSound()
        {
            PlaySound("button");
        }

        /// <summary>
        /// 播放提示音效
        /// </summary>
        public void PlayHintSound()
        {
            PlaySound("hint");
        }

        /// <summary>
        /// 播放指定音效
        /// </summary>
        private void PlaySound(string soundName)
        {
            if (!isEnabled)
                return;

            try
            {
                if (soundPlayers.ContainsKey(soundName))
                {
                    var player = soundPlayers[soundName];
                    if (player != null)
                    {
                        player.Play();
                    }
                }
                else
                {
                    // 如果没有找到指定音效，播放系统音效
                    PlaySystemSound();
                }
            }
            catch (Exception)
            {
                // 如果播放失败，忽略错误
            }
        }

        /// <summary>
        /// 播放系统音效
        /// </summary>
        private void PlaySystemSound()
        {
            try
            {
                SystemSounds.Asterisk.Play();
            }
            catch (Exception)
            {
                // 如果系统音效也失败，则静音
            }
        }

        /// <summary>
        /// 启用或禁用音效
        /// </summary>
        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (isEnabled != value)
                {
                    isEnabled = value;
                    OnSoundEnabledChanged();
                }
            }
        }

        /// <summary>
        /// 音量设置（0.0 - 1.0）
        /// </summary>
        public float Volume
        {
            get { return volume; }
            set
            {
                volume = Math.Max(0.0f, Math.Min(1.0f, value));
            }
        }

        /// <summary>
        /// 触发音效启用状态改变事件
        /// </summary>
        private void OnSoundEnabledChanged()
        {
            if (SoundEnabledChanged != null)
            {
                SoundEnabledChanged(this, isEnabled);
            }
        }

        /// <summary>
        /// 停止所有音效
        /// </summary>
        public void StopAllSounds()
        {
            foreach (var player in soundPlayers.Values)
            {
                try
                {
                    if (player != null)
                    {
                        player.Stop();
                    }
                }
                catch (Exception)
                {
                    // 忽略停止音效时的错误
                }
            }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            StopAllSounds();
            
            foreach (var player in soundPlayers.Values)
            {
                try
                {
                    if (player != null)
                    {
                        player.Dispose();
                    }
                }
                catch (Exception)
                {
                    // 忽略释放资源时的错误
                }
            }
            
            soundPlayers.Clear();
        }
    }
} 